package com.hontech.bread.ui.parts

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.hontech.bread.R
import com.hontech.bread.device.mdb.Bill
import com.hontech.bread.device.mdb.BillAccept
import com.hontech.bread.device.mdb.BillModelInfoLevel1
import com.hontech.bread.device.mdb.BillSetupInfo
import com.hontech.bread.device.mdb.country
import com.hontech.bread.ui.dialog.BillAcceptDialog
import com.hontech.bread.ui.dialog.runProg
import com.hontech.bread.ui.dialog.showInfo
import com.hontech.bread.ui.fragment.MdbBillFragment
import com.hontech.bread.ui.view.LabelTextView
import com.hontech.bread.utils.EmptyEdge
import com.hontech.bread.utils.log
import com.hontech.bread.utils.onClick
import com.hontech.bread.utils.registerEvent
import com.hontech.bread.utils.runOnUi
import com.hontech.bread.utils.stringById
import com.hontech.bread.utils.unregister
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class BillInfoParts(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val rv: RecyclerView

    private val ltLevel: LabelTextView
    private val ltCountry: LabelTextView
    private val ltScaling: LabelTextView
    private val ltDecimal: LabelTextView
    private val ltCapacity: LabelTextView
    private val ltSecurity: LabelTextView
    private val ltEscrow: LabelTextView

    private val man: LabelTextView
    private val serial: LabelTextView
    private val model: LabelTextView
    private val version: LabelTextView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_bill_info, this)

        val btnInit = view.findViewById<Button>(R.id.id_bill_info_btn_init)
        btnInit.onClick { onClickInit() }

        rv = view.findViewById(R.id.id_bill_info_rv)
        rv.layoutManager = FlexboxLayoutManager(context, FlexDirection.ROW, FlexWrap.WRAP)
        rv.edgeEffectFactory = EmptyEdge()

        ltLevel = view.findViewById(R.id.id_bill_info_lt_level)
        ltCountry = view.findViewById(R.id.id_bill_info_lt_country)
        ltScaling = view.findViewById(R.id.id_bill_info_lt_scaling)
        ltDecimal = view.findViewById(R.id.id_bill_info_lt_decimal)
        ltCapacity = view.findViewById(R.id.id_bill_info_lt_capacity)
        ltSecurity = view.findViewById(R.id.id_bill_info_lt_security)
        ltEscrow = view.findViewById(R.id.id_bill_info_lt_escrow)
        man = view.findViewById(R.id.id_bill_info_lt_man)
        serial = view.findViewById(R.id.id_bill_info_lt_serial)
        model = view.findViewById(R.id.id_bill_info_lt_model)
        version = view.findViewById(R.id.id_bill_info_lt_version)
    }

    private suspend fun onClickInit() {
        runProg(
            "${stringById(R.string.t1733460848_386)}",
            "${stringById(R.string.t1733460848_387)}"
        ) {
            Bill.reset()
            rv.adapter = BillAdapter(BillSetupInfo.get().infos())
            val setup = BillSetupInfo.get()
            ltLevel.set(setup.level)
            ltCountry.set(country(setup.countryCode))
            ltScaling.set(setup.scaling)
            ltSecurity.set(setup.security)
            ltDecimal.set(setup.decimalPlaces)
            ltCapacity.set(setup.capacity)
            ltEscrow.set(
                if (setup.isEscrow()) "${stringById(R.string.t1733460848_388)}" else "${
                    stringById(
                        R.string.t1733460848_389
                    )
                }"
            )
            val modelInfo = BillModelInfoLevel1.get()
            man.set(modelInfo.manufacturerCode)
            serial.set(modelInfo.serialNumber)
            model.set(modelInfo.model)
            version.set(modelInfo.softVersion)
        }
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == VISIBLE) {
            registerEvent(this)
        } else {
            unregister(this)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onBillAccept(event: BillAccept) {
        if (!MdbBillFragment.mdbPageFlag) {
            log("bill info parts ${stringById(R.string.t1733460848_390)}")
            return
        }

        runOnUi {
            if (event.isAccept()) {
                val r = BillAcceptDialog().show(
                    "${stringById(R.string.t1733460848_391)}:${event.value()} ${
                        stringById(R.string.t1733460848_392)
                    }?"
                )
                if (r) {
                    Bill.escrow(1)
                } else {
                    Bill.escrow(0)
                }
            } else {
                showInfo(stringById(R.string.t1733460848_393), event.toString())
            }
        }
    }
}


class BillItemView(view: View) : RecyclerView.ViewHolder(view) {

    private val ltValue = view.findViewById<LabelTextView>(R.id.id_item_bill_credit)
    private val btnDisable = view.findViewById<Button>(R.id.id_item_bill_disable)
    private val btnEnable = view.findViewById<Button>(R.id.id_item_bill_enable)

    fun setUp(info: BillSetupInfo.Info) {
        ltValue.set(info.value)
    }
}

class BillAdapter(private val infos: ArrayList<BillSetupInfo.Info>) :
    RecyclerView.Adapter<BillItemView>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BillItemView {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_bill, parent, false)
        return BillItemView(view)
    }

    override fun onBindViewHolder(holder: BillItemView, position: Int) {
        val info = infos[position]
        holder.setUp(info)
    }

    override fun getItemCount(): Int {
        return infos.size
    }
}

